//
//  QChooseDateVC.m
//  QoilPublic
//
//  Created by 杨凯 on 16/8/18.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QChooseDateVC.h"
#import "QChargeBookDB.h"

@interface QChooseDateVC ()

@property(nonatomic,strong)UISegmentedControl *segCtrl;

@end

@implementation QChooseDateVC{

    UIView *_changeOutView;

    UIView *_timeView;
    UIView *_dateView;
    NSDate *_date;
    UIDatePicker *datePicker1;
    UIDatePicker *datePicker2;
    
    NSString *year;
    NSString *month;
    NSString *day;
    NSString *week;
    NSString *time;
    NSString *interval;//时间戳
}

- (instancetype)initWithBlock:(DateBlock)block{

    self = [super init];
    if(self){
        _block = block;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置时间";
    [self.view addSubview:self.segCtrl];
    [self createUI];
    
    week = [QChargeBookDB currentWeek];
    NSDateFormatter *datefot = [[NSDateFormatter alloc] init];
    datefot.dateFormat = @"YYYY/MM-dd HH:mm";
    [self formatDate:[datefot stringFromDate:[NSDate date]]];
    NSTimeInterval inter = [[NSDate date] timeIntervalSince1970] * 1000;//当前时间戳
    interval = [NSString stringWithFormat:@"%.f",inter];
}

- (void)createUI{

    _changeOutView = [[UIView alloc] initWithFrame:CGRectMake(0, self.segCtrl.bottom+20, kScreenWidth, 200)];
    [self.view addSubview:_changeOutView];
    
    _timeView = [[UIView alloc] initWithFrame:_changeOutView.bounds];
    _timeView.backgroundColor = [UIColor whiteColor];
    datePicker1 = [[UIDatePicker alloc]initWithFrame:_timeView.bounds];
    [datePicker1 setTimeZone:[NSTimeZone defaultTimeZone]];
    [datePicker1 setDatePickerMode:UIDatePickerModeDateAndTime];
    datePicker1.tag = 1;
    [datePicker1 addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
    [_timeView addSubview:datePicker1];
    [_changeOutView addSubview:_timeView];

    _dateView = [[UIView alloc] initWithFrame:_changeOutView.bounds];
    _dateView.backgroundColor = [UIColor whiteColor];
    _dateView.hidden = YES;
    datePicker2 = [[UIDatePicker alloc]initWithFrame:_dateView.bounds];
    [datePicker2 setTimeZone:[NSTimeZone defaultTimeZone]];
    [datePicker2 setDatePickerMode:UIDatePickerModeDate];
    datePicker2.countDownDuration = 1;
    datePicker2.tag = 2;
    [datePicker2 addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
    [_dateView addSubview:datePicker2];
    [_changeOutView addSubview:_dateView];
}

- (UISegmentedControl*)segCtrl {
    if (!_segCtrl) {
        _segCtrl = [[UISegmentedControl alloc]initWithItems:@[@"时间",@"日期"]];
        _segCtrl.frame = CGRectMake(20, kNavHeight+kStatusHeight+20, kScreenWidth-40, 35);
        _segCtrl.tintColor = [UIColor grayColor];
        _segCtrl.selectedSegmentIndex = 0;
        [_segCtrl addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    }
    return _segCtrl;
}

- (void)segAction:(UISegmentedControl*)sender {
    CLog(@"%ld",(long)sender.selectedSegmentIndex);
 
    if(sender.selectedSegmentIndex == 1){
        _dateView.hidden = NO;
        _timeView.hidden = YES;
        if(_date){
          [datePicker2 setDate:_date];
        }
    }else{
        _dateView.hidden = YES;
        _timeView.hidden = NO;
        if(_date){
          [datePicker1 setDate:_date];
        }
    }
}

- (void)datePickerValueChanged:(UIDatePicker*)datePicker {
    CLog(@"===%ld",(long)datePicker.tag);
    if(datePicker.tag == 1){
        _date = datePicker.date;
        NSDateFormatter *datefot = [[NSDateFormatter alloc] init];
        datefot.dateFormat = @"YYYY/MM-dd HH:mm";
        NSString *str = [datefot stringFromDate:datePicker.date];
        [self formatDate:str];
        NSString *w = [self getWeekDayFordate:datePicker.date];
        week = w;
    }else if (datePicker.tag == 2){
       _date = datePicker.date;
        NSDateFormatter *datefot = [[NSDateFormatter alloc] init];
        datefot.dateFormat = @"YYYY/MM-dd HH:mm";
        NSString *str = [datefot stringFromDate:datePicker.date];
        [self formatDate:str];
        NSString *w = [self getWeekDayFordate:datePicker.date];
        week = w;
    }
    
    NSTimeInterval inter = [_date timeIntervalSince1970] * 1000;
    interval = [NSString stringWithFormat:@"%.f",inter];
    
    if(_block){
        _block(year,month,day,week,interval,time);
    }
}

- (NSString *)getWeekDayFordate:(NSDate *)date{
    NSArray *weekday = @[@"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六",@""];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitWeekday|NSCalendarUnitDay fromDate:date];
    NSString *weekStr = [weekday objectAtIndex:components.weekday-1];
    return weekStr;
}

#pragma mark----赋值年月日
- (void)formatDate:(NSString *)str{
    [QChargeBookDB formatDate:str successBlock:^(NSString *y, NSString *m, NSString *d, NSString *t) {
        year = y;
        month = m;
        day = d;
        time = t;
    }];
}

@end
